import 'package:cbec_app/entity/home/content/sign_in_home_data.dart';
import 'package:flutter/material.dart';
import 'package:flutter_popup/flutter_popup.dart';
import 'package:intl/intl.dart';

import '../../../utils/Tools.dart';
import '../../components/decoration/decoration_rich_text_parse.dart';
import '../../entity/signIn/sign_in_recored_bean.dart';
import '../../utils/AppData.dart';

class SignInWidget extends StatefulWidget {
  final SignInRecoredBean? signInData;
  final Function? onPress;

  ///富文本配置
  final Map<String, dynamic> mapParam;

  SignInWidget(
      {Key? key, this.signInData, this.onPress, required this.mapParam});

  @override
  _SignInWidgetState createState() => _SignInWidgetState();
}

class _SignInWidgetState extends State<SignInWidget>
    with TickerProviderStateMixin {
  SignInHomeData? data;

  bool enableClick = false;

  _SignInWidgetState() {}

  @override
  void initState() {
    data = SignInHomeData.fromJson(widget.mapParam);

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    DateTime time = DateTime.now();
    if (widget.signInData?.signType == 1) {
      if (widget.signInData?.signRecords?[time.weekday - 1].signIn ?? false) {
        enableClick = false;
      } else {
        enableClick = true;
      }
    } else if (widget.signInData?.signType == 2) {
      if (widget.signInData?.signRecords?[time.day - 1].signIn ?? false) {
        enableClick = false;
      } else {
        enableClick = true;
      }
    } else if (widget.signInData?.signType == 3) {
      String currentTime = DateFormat("yyyy-MM-dd").format(time);
      enableClick = true;
      widget.signInData?.signRecords?.forEach((e) {
        if (e.signInDate == currentTime &&
            e.signIn != null &&
            e.signIn == true) {
          enableClick = false;
        }
      });
    }
    return Container(
        padding: EdgeInsets.only(
            top: data!.pageMarginTop!.toDouble(),
            right: data!.pageMarginRight!.toDouble(),
            left: data!.pageMarginLeft!.toDouble(),
            bottom: data!.pageMarginBottom!.toDouble()),
        width: Screen.width,
        color: Tools.fromHex(data!.bgColor!),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              '${data?.title}',
              style: TextStyle(
                  color: Tools.fromHex('${data!.titleColor}'),
                  fontWeight: Tools.getWeight(data!.titleWeight ?? ""),
                  fontSize: double.parse(data!.titleSize!)),
            ),
            Text(
              '${data?.desc}',
              style: TextStyle(
                  color: Tools.fromHex('${data!.descColor}'),
                  fontWeight: Tools.getWeight(data!.descWeight ?? ""),
                  fontSize: double.parse(data!.descSize!)),
            ),
            SizedBox(
              height: 5,
            ),
            Row(
              children: [
                Container(
                  width: 140,
                  child: Stack(
                    children: [
                      Image.asset("assets/images/ic_sign_in_mark_bg.png"),
                      Container(
                        width: 140,
                        alignment: Alignment.center,
                        padding: EdgeInsets.only(top: 10),
                        child: Text(
                          '累计签到${widget.signInData?.continuousTotal ?? 0}天',
                          style: TextStyle(color: Colors.white),
                        ),
                      )
                    ],
                  ),
                ),
                Expanded(
                    child: SizedBox(
                  width: 1,
                )),
                GestureDetector(
                  onTap: () {
                    _showRuleDialog();
                  },
                  child: Row(
                    children: [
                      Text(
                        '规则说明',
                        style: TextStyle(color: Colors.black, fontSize: 10),
                      ),
                      Image.asset(
                        "assets/images/ic_sign_in_rule.png",
                        width: 14,
                      ),
                    ],
                  ),
                )
              ],
            ),
            _signInLayout(),
            SizedBox(
              height: 10,
            ),
            Container(
              alignment: Alignment.center,
              child: GestureDetector(
                onTap: () {
                  if (!AppData.isLogin) {
                    Tools.showToast('请登录');
                    return;
                  } else if (!enableClick) {
                    Tools.showToast("已签到");
                  } else {
                    widget.onPress?.call();
                  }
                },
                child: Card(
                  color: enableClick
                      ? Tools.fromHex('${data!.btnBgcolor}')
                      : Tools.fromHex('${data!.btnBgcolorend}'),
                  child: Container(
                    width: 100,
                    alignment: Alignment.center,
                    padding: EdgeInsets.symmetric(vertical: 8),
                    child: Text(
                      '${data!.btnText}',
                      style: TextStyle(
                          color: Tools.fromHex('${data!.btnTextColor}'),
                          fontSize: 14),
                    ),
                  ),
                ),
              ),
            ),
          ],
        ));
  }

  Widget _signInLayout() {
    // if (widget.data?.signType == 1 || widget.data?.signType == 2) {
    return Container(
      width: Screen.width,
      child: Wrap(
        alignment: WrapAlignment.start,
        runSpacing: 4,
        children: List.generate(
          widget.signInData?.signRecords?.length ?? 0,
          (index) => Container(
              width: (Screen.width -
                      data!.pageMarginRight!.toDouble() -
                      data!.pageMarginLeft!.toDouble()) /
                  6,
              child: Column(
                children: [
                  Image.asset(
                    'assets/images/ic_sign_in_item.png',
                    width: 30,
                  ),
                  //
                  Card(
                      color: Tools.fromHex('#F4742C'),
                      child: Container(
                        padding: EdgeInsets.symmetric(horizontal: 3),
                        child: GestureDetector(
                            onTap: () {},
                            child: Text(
                              widget.signInData?.signRecords![index].signIn ??
                                      false
                                  ? "已领取"
                                  : "待领取",
                              style:
                                  TextStyle(fontSize: 10, color: Colors.white),
                            )),
                      )),
                  Text(
                    '${widget.signInData?.signRecords![index].dayName}',
                    style: TextStyle(
                        fontSize: 10, color: Tools.fromHex('#666666')),
                  )
                ],
              )),
        ),
      ),
    );
  }

  void _showRuleDialog() {
    showDialog(
        context: context,
        builder: (c) {
          Map<String, dynamic> param = Map();
          param['color'] = '#ffffff';
          param['content'] = widget.signInData?.content ?? "";
          return Dialog(
            backgroundColor: Colors.transparent,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Container(
                  padding: EdgeInsets.all(6),
                  width: MediaQuery.of(context).size.width,
                  height: 200,
                  decoration: BoxDecoration(
                    color: Tools.fromHex('#FFFFFF'), // 背景颜色
                    borderRadius: BorderRadius.circular(8), // 设置圆角
                  ),
                  child: DecorationRichTextParse(info: param),
                ),
                SizedBox(
                  height: 18,
                ),
                GestureDetector(
                  onTap: () {
                    Navigator.pop(c);
                  },
                  child: Image.asset(
                    "assets/images/close.png",
                    color: Colors.white,
                  ),
                )
              ],
            ),
          );
        });
  }
}
